package com.service.impl;

import com.dao.UserMapper;
import com.entity.Data;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
     private  UserMapper userMapper;
    public Object login(User user){
        User u = userMapper.login(user);
        Data data = new Data();
        if (u == null){
            data.setState(false);
            data.setError("密码错误或账号不存在");
            return data;
        }
        data.setState(true);
        data.setData(u);
        return data;
    }
    public Object userRegister(User user){
        Data data = new Data();
        try {
           user.setUserID( UUID.randomUUID().toString());
            userMapper.userRegister(user);
        } catch (Exception e) {
            data.setState(false);
            data.setError("用户名已存在");
            return data;
        }
        data.setState(true);
        return data;
    }
    public Object updateUserPwd(String userName,String pwd,String newPwd){
        Data data = new Data();
        User user = new User();
        user.setUserName(userName);
        user.setPwd(pwd);
        if (userMapper.login(user) == null){
            data.setState(false);
            data.setError("原密码错误！");
            return data;
        }
        user.setPwd(newPwd);
            try {
                if(userMapper.updateUserPwd(user) == 1){
                    data.setState(true);
                    return data;
                }else {
                    data.setState(false);
                    data.setError("修改失败！");
                    return data;
                }
            }catch (Exception e){
                e.printStackTrace();
                data.setState(false);
                data.setError("修改失败！");
                return data;
            }
    }
}
